import {useMessage} from "/@/hooks/message";

/**
 * 读卡
 * @param WsUrl 读卡器地址
 * @param callback 回调函数
 */
export function sendReadCard(WsUrl:any, callback:any) {
    let ws = new WebSocket(WsUrl);
    ws.addEventListener("open", () => {
        ws.send("读卡");
    });

    ws.addEventListener("close", () => {
    });

    ws.addEventListener("error", () => {
        ws.close();
        useMessage().success('读卡失败');
    });

    ws.addEventListener("message", (message) => {
        if (message.data !== "Open!") {
            callback(message.data);
            ws.close();
        }
    });
}


/**
 * 打印列表
 * @param WsUrl 打印机地址
 * @param str 打印内容
 */
export function printList(WsUrl:any, str:any) {
    let ws = new WebSocket(WsUrl);
    ws.addEventListener("open", () => {
        ws.send(str);
        ws.close();
    });

    ws.addEventListener("close", () => {
    });

    ws.addEventListener("error", () => {
        ws.close();
    });

    ws.addEventListener("message", () => {
    });
}

